//
//  YXYOriginalView.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/27.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class YXYOriginalView: UIView {
    
    // 底部的约束
    var originalBottomConstraint: Constraint?
    
    // 供外界赋值
    var status: YXYStatus? {
        didSet {
            headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default"))
            nameLabel.text = status?.user?.name
            expiredImageView.image = status?.user?.mbrankImage
            
            timeLabel.text = status?.creatTimeStr
            sourceLabel.attributedText = status?.sourceStr
            VimageView.image = status?.user?.verifiedImage
            contentLabel.text = status?.text
            
            // 卸载约束
            originalBottomConstraint?.uninstall()
            
            // 判断图片视图是否显示，并且赋值
            if status?.pic_urls?.count > 0 {
                pictureView.hidden = false
                // 赋值
                pictureView.pic_urls = status?.pic_urls
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    originalBottomConstraint = make.bottom.equalTo(pictureView).offset(constantMargin).constraint
                })
            }else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    originalBottomConstraint = make.bottom.equalTo(contentLabel).offset(constantMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 创建子控件
    private func setUpUI () {
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(VimageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        setConstraint()
    }
    // MARK: - 设置约束
    private func setConstraint () {
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(constantMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(constantMargin)
        }
        expiredImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(constantMargin)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(constantMargin)
        }
        VimageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(constantMargin)
            make.leading.equalTo(self).offset(constantMargin)
            make.trailing.equalTo(self).offset(-constantMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(constantMargin)
            //make.size.equalTo(CGSize(width: 100, height: 100))
            make.leading.equalTo(self).offset(constantMargin)
        }
        
        // 由于要使用自动计算行高的功能，所以要设置self的约束
        self.snp_makeConstraints { (make) -> Void in
            originalBottomConstraint = make.bottom.equalTo(pictureView).offset(constantMargin).constraint
        }
        
    }
    
    
    // 头像
    lazy var headImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "avatar_default_big"))
        return img
    }()
    // 昵称
    lazy var nameLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.darkGrayColor()
        lab.font = UIFont.systemFontOfSize(15)
        return lab
    }()
    // 会员等级
    lazy var expiredImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
        return img
    }()
    // 微博创建时间
    lazy var timeLabel: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(10)
        lab.textColor = themeColor
        return lab
    }()
    // 微博来源
    lazy var sourceLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.font = UIFont.systemFontOfSize(10)
        return lab
    }()
    // 微博认证
    lazy var VimageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "avatar_grassroot"))
        return img
    }()
    // 微博内容
    lazy var contentLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.blackColor()
        lab.font = UIFont.systemFontOfSize(14)
        lab.numberOfLines = 0
        return lab
    }()
    // 图片视图
    lazy var pictureView: YXYPictureView = YXYPictureView()
}
